//@author A0084731E
import java.util.*;

public class BudgetPlanner extends Transactions{

	// ATTRIBUTES
	private List<BudgetPlannerItem> _chosen;
	private double _budget, _remaining;
	private PriorityComparator _comp;

	// CONSTRUCTOR
	public BudgetPlanner(double budget) {
		this._budget = budget;
		this._remaining = budget;
		this._chosen = new ArrayList<BudgetPlannerItem>();
		this._comp = new PriorityComparator();
	}

	// METHODS
	// GETTER
	public double getBudget() {	
		return this._budget;	
	}
	public double getRemainingBudget() {	
		return this._remaining;	
	}
	public List<Transaction> getResult() {	
		List<Transaction> result = new ArrayList<Transaction>();
		for(int i=0;i<this.getChosen().size();i++)
			result.add(this.getChosen().get(i));

		return result; 	
	}
	public List<BudgetPlannerItem> getChosen() {
		return this._chosen;
	}
	
	public BudgetPlannerItem getItem(int id) {
		return (BudgetPlannerItem) super.getTransaction(id);
	}
	
	// SETTER
	public void setBudget(double budget) {	
		this._budget = budget; 
		this._remaining = budget;	
	}

	// Sort the items in descending order of priority value
	public void maintainOrder() {
		Collections.sort(super.getTransactions(), _comp);
	}

	// Add item
	public boolean addItem(BudgetPlannerItem item) {
		super.addTransaction(item);
		maintainOrder();
		return true;
	}
	// Update item
	public Transaction updateItem(int id, BudgetPlannerItem bp) { 
		Transaction temp = super.updateTransaction(id, bp);
		maintainOrder();
		return temp;
	}
	// Delete item
	public boolean deleteItem(int id) {
		super.getTransactions().remove(id);
		maintainOrder();
		return true;
	}

	// Clear
	public boolean clear() {
		this.setBudget(0);
		super.getTransactions().clear();
		this.getChosen().clear();
		return true;
	}

	// Make decision
	public List<Transaction> decide() {
		this.getChosen().clear();		

		for(int i=0;i<super.size();i++){
			BudgetPlannerItem item = (BudgetPlannerItem)super.getTransactions().get(i);
			if(item.getValue()<= this.getRemainingBudget()) {
				this.getChosen().add(item);
				this._remaining -= item.getValue();
			}	
		}

		return this.getResult();
	}

	// The decision is adapted
	public boolean added() {
		super.getTransactions().removeAll(this.getChosen());
		this._budget = this.getRemainingBudget();
		return true;
	}
	// The decision is abandoned
	public boolean notAdded(){
		this._remaining = this.getBudget();
		return true;
	}

	// Printing method
	public  void print() {
		System.out.println("ID"+"  "+"LABEL"+" "+"VALUE"+" "+"CATEGORY"+" "+"REMARKS"+" "+"PRIORITY");
		for (int i=0;i<super.size();i++) {
			System.out.println(super.getTransactions().get(i).toString());
		}
		System.out.println("Remaining Budget: "+this.getBudget());				
	}
	
	// Change currency
	public void ConvertCurrency(String newCurrency) {		 
		double oldExRate = super.getExRate().get(super.getCurrency());
		double newExRate = super.getExRate().get(newCurrency);
		
		for (int i=0;i<super.size();i++) {
			Transaction transaction = super.getTransactions().get(i);
			transaction.setValue(transaction.getValue()/oldExRate*newExRate);			
		}
		this._budget = this._budget/oldExRate*newExRate;
		super.setCurrency(newCurrency);
	}
}
